package com.atguigu.oss.controller;

import com.atguigu.commonutils.R;
import com.atguigu.oss.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@Api(description="阿里云文件管理")
//@CrossOrigin //跨域
@RestController
@RequestMapping("/eduOSS/uploadFile")
public class FileController {

	@Autowired
	private FileService fileService;

	@Autowired
	private RedisTemplate<String,String> redisTemplate;

	/**
	 * 文件上传
	 *
	 * @param file
	 */
	@ApiOperation(value = "文件上传")
	@PostMapping("upload")
	public R upload(@RequestParam("file") MultipartFile file) {

		System.out.println(file);

		String uploadUrl = fileService.upload(file);
		//返回r对象
		return R.ok().message("文件上传成功").data("url", uploadUrl);

	}


	@ApiOperation(value = "文件上传2")
	@PostMapping("uploadImage")
	public R uploadImage(@RequestParam(value = "file") MultipartFile file) throws IOException {

		File iofile = null;
		if (file.equals("") || file.getSize() <= 0) {
			iofile = null;
		} else {
			InputStream ins = null;
			ins = file.getInputStream();
			iofile = new File(file.getOriginalFilename());
			inputStreamToFile(ins, iofile);
			ins.close();
		}

		InputStream inputStream = new FileInputStream(iofile);
		MultipartFile multipartFile = new MockMultipartFile(iofile.getName(), inputStream);

		String uploadUrl = fileService.uploadImage(multipartFile,iofile.getName());

		System.out.println(uploadUrl);

		String key="MemberAvatar";
		redisTemplate.opsForValue().set(key,uploadUrl);

		//删除缓存
		delteTempFile(iofile);


		return R.ok().data("url",uploadUrl);
	}

	@ApiOperation(value = "文件上传")
	@PostMapping("uploadone")
	public R uploadOne(@RequestParam("file") MultipartFile[] file) {

		System.out.println(file[0]);

		String uploadUrl = fileService.upload(file[0]);
		//返回r对象
		return R.ok().message("文件上传成功").data("url", uploadUrl);

	}


	@ApiOperation(value = "文件上传")
	@PostMapping("uploadAvatar")
	public R uploadAvatar(@RequestParam("avatarfile") MultipartFile file) {

		System.out.println(file);

		String uploadUrl = fileService.upload(file);
		//返回r对象
		return R.ok().message("文件上传成功").data("url", uploadUrl);

	}


	//获取流文件
	private static void inputStreamToFile(InputStream ins, File file) {
		try {
			OutputStream os = new FileOutputStream(file);
			int bytesRead = 0;
			byte[] buffer = new byte[8192];
			while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
			os.close();
			ins.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除本地临时文件
	 * @param file
	 */
	public static void delteTempFile(File file) {
		if (file != null) {
			File del = new File(file.toURI());
			del.delete();
		}
	}
}
